<?php

namespace app\admin\controller\purchase;

use app\common\controller\Backend;
use think\Db;

/**
 * 请购单
 *
 * @icon fa fa-circle-o
 */
class Requisition extends Backend
{
    
    /**
     * Requisition模型对象
     * @var \app\admin\model\purchase\Requisition
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\purchase\Requisition;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function adds(){
        $group = Db::name('auth_group_access')->alias('a')->where(array('uid'=>session('admin')['id']))->join('zax_auth_group g','a.group_id = g.id')->find();

        if ($this->request->isAjax())
        {
            $params = input();
            $gdata = $params['row'];
            if(empty($gdata)){
                $this->error('未添加商品');
            }
            $createtime = strtotime($params['createtime']);
            $needtime = strtotime($params['needtime']);
            $abstract = strtotime($params['abstract']);
            $explain = strtotime($params['explain']);

            Db::startTrans();
            try{
                $num = $this->model->where(array('createtime'=>array('>',strtotime(date('Y-m-d',time())))))->count();
                $number = 'QG-T-'.date('Y-m-d').'-'.getnumber($num);
                $pdata = array(
                   'number'=>$number,
                   'createtime'=>$createtime,
                   'admin_id'=>session('admin')['id'],
                   'explain'=>$explain,
                   'abstract'=>$abstract,
                   'group_id'=>$group['group_id'],
                   'needtime'=>$needtime,
                );

                $res = $this->model->save($pdata);
                if(!$res){
                    Db::rollback();
                    $this->error('添加失败');
                }
                $ggdata = [];
                foreach ($gdata as $v){
                    $ggdata[] = array(
                      'purchase_requisition_id'=>$this->model->id,
                      'goods_specs_id'=>$v['specs_ids'],
                      'requisition_num'=>$v['numbers'],
                      'remark'=>$v['remark'],
                      'price'=>empty($v['unit_price'])?0:$v['unit_price'],
                    );
                }
                Db::name('purchase_requisition_goods')->insertAll($ggdata);
                 //提交事务
                Db::commit();
                return array(
                    'code'=>1,
                    'msg'=>'添加成功',
                );
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->error('添加失败');
            }


            exit;
        }

        $this->assign('nickname',session('admin')['nickname']);
        $this->assign('gname',$group['name']);
        $arr = explode('/',$_SERVER['REQUEST_URI']);
        $this->assign('turl', handlePrefix().'://'.$_SERVER['SERVER_NAME'].'/'.$arr[1]);
        return $this->view->fetch();
    }
    public function table1(){
        $sort = input('sort');
        $order = input('order');
        $offset = input('offset');
        $limit = input('limit');
        $list = $this->model->limit($offset,$limit)->order($sort,$order)->select();
        foreach ($list as $key => $v){
            $group = Db::name('auth_group')->where(array('id'=>$v['group_id']))->find();
            $admininfo = Db::name('admin')->where(array('id'=>$v['admin_id']))->find();
            $list[$key]['nickname'] = $admininfo['nickname'];
            $list[$key]['name'] = $group['name'];
        }
        $total = $this->model->count();
        $data = array('rows'=>$list,'total'=>$total);
        return $data;
    }
    public function table2(){
        $sort = input('sort');
        $order = input('order');

        $filter = $this->request->get("filter");
        $filter = (array)json_decode($filter, true);
        if(!empty($filter)){
            foreach ($filter as $key =>$value){
                if($key == 'number'){
                    $rinfo = $this->model->where(array('id'=>$value))->find();
                    $where = array('purchase_requisition_id'=>$rinfo['id']);
                }
            }
        }else{
            $rinfo = $this->model->order($sort,$order)->find();
            $where = array('purchase_requisition_id'=>$rinfo['id']);
        }

        $rows = Db::name('purchase_requisition_goods')
            ->where($where)
            ->alias('gs')
            ->field('gs.id,g.title,s.specs,gs.requisition_num,gs.price')
//            ->join('purchase_requisition_goods gs','r.id=gs.purchase_requisition_id')
            ->join('goods_specs s','gs.goods_specs_id = s.id')
            ->join('goods g','s.goods_id = g.id')

//            ->order('r.id',$order)
            ->select();

        $total = Db::name('purchase_requisition_goods')
            ->where($where)
            ->alias('gs')
            ->field('gs.id,g.title,s.specs,gs.requisition_num')
            ->join('goods g','gs.goods_id = g.id')
            ->join('goods_specs s','gs.goods_specs_id = s.id')
            ->count();
        $data = array('rows'=>$rows,'total'=>$total);
        return $data;
    }


}
